// 绑定保存按钮事件
function bindEventForSaveBtn($form, $taskMaxNumber, $batchNumber, $threadNumber){
	$("#systemConfigSubBtn").click(function() {
		var isSuccess = true;
		// 对登录表单进行非空校验
		$form.find("input.required").each(function() {
			if ($(this).val() != "") {
				$(this).removeClass("driverVerifcateError");
				return;
			}
			$(this).addClass("driverVerifcateError");
			isSuccess = false;
		});
		// 触发 keyup 事件
		$form.find("input.required").keyup(function() {
			$(this).removeClass("driverVerifcateError");
			if ($(this).val() == "") {
				$(this).addClass("driverVerifcateError");
			}
		});
		//如果验证不成功，则返回false
		if (!isSuccess) return false;
		
		// 点击确定确认登录请求后台
		var data = {
			"fastsyncer.worker.task.max.number" : $taskMaxNumber.val(),
			"fastsyncer.worker.batch.number" : $batchNumber.val(),
			"fastsyncer.worker.thread.number" : $threadNumber.val()
		};
		
		$.post($basePath + "/system/update", {
			"params" : JSON.stringify(data)
		}, function(data) {
			if (data.success == true) {
				bootGrowl(data.resultValue, "success");
			} else {
				// 请求失败
				bootGrowl(data.resultValue,"danger");
			}
		});
	});
};

// 初始化系统参数
function initSysConfigParams($taskMaxNumber, $batchNumber, $threadNumber) {
	var params = JSON.parse($("#system_params").html());
	var taskMaxNumber = params[SystemParamsTaskMaxNumber];
	var batchNumber = params[SystemParamsBatchNumber];
	var threadNumber = params[SystemParamsThreadNumber];
	
	$taskMaxNumber.val(taskMaxNumber);
	$batchNumber.val(batchNumber);
	$threadNumber.val(threadNumber);
};

$(function() {
	var $form = $("#systemConfigForm");
	var $taskMaxNumber = $form.find('input[name=\"'+SystemParamsTaskMaxNumber+'\"]');
	var $batchNumber = $form.find('input[name=\"'+SystemParamsBatchNumber+'\"]');
	var $threadNumber = $form.find('input[name=\"'+SystemParamsThreadNumber+'\"]');
		
	// 初始化系统参数
	initSysConfigParams($taskMaxNumber, $batchNumber, $threadNumber);

	// 绑定保存按钮事件
	bindEventForSaveBtn($form, $taskMaxNumber, $batchNumber, $threadNumber);
});

